package com.pilot.game.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.pilot.game.domain.Kind;
import com.pilot.game.service.KindService;
import com.pilot.game.web.rest.errors.BadRequestAlertException;
import com.pilot.game.web.rest.util.HeaderUtil;
import com.pilot.game.web.rest.util.PaginationUtil;
import io.swagger.annotations.ApiParam;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Kind.
 */
@RestController
@RequestMapping("/api")
public class KindResource {

    private final Logger log = LoggerFactory.getLogger(KindResource.class);

    private static final String ENTITY_NAME = "kind";

    private final KindService kindService;

    public KindResource(KindService kindService) {
        this.kindService = kindService;
    }

    /**
     * POST  /kinds : Create a new kind.
     *
     * @param kind the kind to create
     * @return the ResponseEntity with status 201 (Created) and with body the new kind, or with status 400 (Bad Request) if the kind has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/kinds")
    @Timed
    public ResponseEntity<Kind> createKind(@RequestBody Kind kind) throws URISyntaxException {
        log.debug("REST request to save Kind : {}", kind);
        if (kind.getId() != null) {
            throw new BadRequestAlertException("A new kind cannot already have an ID", ENTITY_NAME, "idexists");
        }
        Kind result = kindService.save(kind);
        return ResponseEntity.created(new URI("/api/kinds/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /kinds : Updates an existing kind.
     *
     * @param kind the kind to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated kind,
     * or with status 400 (Bad Request) if the kind is not valid,
     * or with status 500 (Internal Server Error) if the kind couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/kinds")
    @Timed
    public ResponseEntity<Kind> updateKind(@RequestBody Kind kind) throws URISyntaxException {
        log.debug("REST request to update Kind : {}", kind);
        if (kind.getId() == null) {
            return createKind(kind);
        }
        Kind result = kindService.save(kind);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, kind.getId().toString()))
            .body(result);
    }

    /**
     * GET  /kinds : get all the kinds.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of kinds in body
     */
    @GetMapping("/kinds")
    @Timed
    public ResponseEntity<List<Kind>> getAllKinds(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of Kinds");
        Page<Kind> page = kindService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/kinds");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /kinds/:id : get the "id" kind.
     *
     * @param id the id of the kind to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the kind, or with status 404 (Not Found)
     */
    @GetMapping("/kinds/{id}")
    @Timed
    public ResponseEntity<Kind> getKind(@PathVariable Long id) {
        log.debug("REST request to get Kind : {}", id);
        Kind kind = kindService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(kind));
    }

    /**
     * DELETE  /kinds/:id : delete the "id" kind.
     *
     * @param id the id of the kind to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/kinds/{id}")
    @Timed
    public ResponseEntity<Void> deleteKind(@PathVariable Long id) {
        log.debug("REST request to delete Kind : {}", id);
        kindService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
